<?php
/*
 * 
 * PAGE DES SHORTCODES
 * 
 */
function wpspo_button_shortcode( $atts ) {

	// Attributes
	extract( shortcode_atts(
		array(
			'link' => get_post_permalink(),
            'onclick' => '',
			'color' => 'orange',
			'taille' => 'medium',
            'bgcouleur' => '',
            'couleurtexte' => '',
            'content' => 'bouton',
            'target' => '_self'
		), $atts )
	);
    if($_GET['projet']) { 
        if( $onclick ) { 
            $onclick = 'onclick="'.$lonclick.'"';
            $addLink = '?projet='.$_GET['projet'].'#valide';
        } else {
            //$addLink = '&projet='.$_GET['projet'];
            $addLink = '';
            $onclick = '';
        }
      
    }
	// Code
	return '<a style="background-color:'.$bgcouleur.';color:'.$couleurtexte.';" class="wpspo-bouton '.$taille.' '.$color.'" href="'.$link.''.$addLink.'" onclick="'.$onclick.'" target="'.$target.'">'.$content.'</a>';
}
add_shortcode( 'wpspobutton', 'wpspo_button_shortcode' );

/*
 * BOUTON FORM
 *  */
function wpspo_button_form_shortcode( $atts ) {

	// Attributes
	extract( shortcode_atts(
		array(
            'textconfirm' => '',
			'color' => 'orange',
			'taille' => 'medium',
            'bgcouleur' => '',
            'couleurtexte' => '',
            'content' => 'bouton'
		), $atts )
	);
	// Code    
    $btnForm = '<form name="valide_form" method="post">
                <input type="hidden" name="secure" value="true">
                <a style="background-color:'.$bgcouleur.';color:'.$couleurtexte.';" class="wpspo-bouton '.$taille.' '.$color.'" href="#" onclick="return ValiderForm(\''.$textconfirm.'\');" target="'.$target.'">'.$content.'</a>
            </form>';

    return $btnForm;
    
}
add_shortcode( 'wpspobuttonform', 'wpspo_button_form_shortcode' );

/* 
 * Shortcode pour affichage profil 
 */
function wpspo_shortcode_profil( $atts ) {
    
	// Attributes
	extract( shortcode_atts(
		array(
			'name' => '0',
			'moncompte' => '0',
		), $atts )
	);
    if ( is_user_logged_in() ) {
        
        global $current_user;
        $roleUser = wpspo_get_user_role();
        $addLinkProfil = '';
        if($roleUser=='participant' or $roleUser=='administrator' ) {
            $addLinkProfil = ' - <a href="'.get_permalink(WPSPO_ID_COMPTE).'" style="text-decoration:underline;">'.get_page(WPSPO_ID_COMPTE)->post_title.'</a> ';
        }
        return ''.__('Welcome', 'sponsorpress').' '.ucfirst($current_user->display_name).''.$addLinkProfil.' - <a href="'.wp_logout_url( home_url() ).'"  style="text-decoration:underline;" title="'.__('Logout', 'sponsorpress').'">'.__('Logout', 'sponsorpress').'</a>';
        
    } else {
        // Code
        return '<a href="'.wp_login_url( home_url() ).'"  style="text-decoration:underline;" title="'.__('Login', 'sponsorpress').'">'.__('Login', 'sponsorpress').'</a> / <a href="'.wp_registration_url().'" style="text-decoration:underline;" title="'.__('Register', 'sponsorpress').'">'.__('Register', 'sponsorpress').'</a>';
    }

}
add_shortcode( 'wpspo_profil', 'wpspo_shortcode_profil' );

/*
 * ShortCode pour les derniers inscrits
 */
// Add Shortcode
function wpspo_lastuser_shortcode( $atts ) {

    global $wpdb;
	// Attributes
	extract( shortcode_atts(
		array(
			'nb' => '4',
            'txt' => 'Dernièrement inscrit :'
		), $atts )
	);

	// Code
	$lastusers .= '<div class="inscrits"><p><strong>'.$txt.'</strong>';
    $usernames = $wpdb->get_results("SELECT display_name, user_nicename FROM $wpdb->users ORDER BY ID DESC LIMIT '.$nb.'");
    foreach ($usernames as $username) {
            $lastusers .='<a href="'.bloginfo('url').'/author/'.$username->user_nicename.'/">'.$username->display_name.'</a> - ';
    }
    $lastusers .='</p></div>';
     
    return  $lastusers;
}
add_shortcode( 'wpspo-lastuser', 'wpspo_lastuser_shortcode' );

/*
 * REtourne une jauge
 */
function wpspo_jauge_shortcode( $atts ) {
    global $wpdb;
    
	// Attributes
	extract( shortcode_atts(
		array(
			'id' => '',
            'type' => 1,
            'text' => '',
            'color' => '#CC0000',
            'display' => 1,
            'choose' => 1,
            'size' => 30,
            'top' => 10,
            'bottom' => 10
		), $atts )
	);
    
    if( isset($id) && is_numeric($id) ) {
        
        if($choose==1) {
            $valueObjectif = get_post_meta($id, 'wpspo_boxprojets', true); // Récupère l'objectif cible
            $totalPromesse = WPSPO_Promesse::get_sum($id);  // Récupère les promesses FIXES
            //$totalPromesse = 1000;
        } else {
            $totalPromesse = WPSPO_Promesse::get_sum($id, 1, 'defi');
        }
        // Récupère le texte et on le traite si il existe
        $textProjet = get_post_meta($id, 'wpspo_boxtexte', true);
        if($textProjet == '') {
            $textProjet = $text;
        }
        if($choose==1) {
            $textProjet = str_replace('%COL%', wpspo_price($totalPromesse), $textProjet);
        } else {
            $textProjet = str_replace('%COL%', '<div style="text-align:center;font-size:35px;">'.wpspo_price($totalPromesse).'</div>', $textProjet);
        }
        $textProjet = str_replace('%OBJ%', wpspo_price($valueObjectif), $textProjet);
        $textProjet = str_replace('%SA%', '<br />', $textProjet);
        
        if($valueObjectif>1) {
            $ratio = round( ($totalPromesse*100)/$valueObjectif );
        } else {
            $ratio = round( $totalPromesse/100 );
        }
        
        if($type == 1 && $valueObjectif>1) {
            
            $printJauge = '<div id="wpspo-cadre">'.$textProjet;
            if($display==1) {
            $printJauge .= '<style type="text/css">
                            progress::-webkit-progress-value {
                              border-radius: 5px;
                              background-color: '.$color.';
                              background-image: -webkit-linear-gradient(-45deg, transparent 25%, rgba(255, 255, 255, 0.3) 25%, rgba(255, 255, 255, 0.3) 50%, transparent 50%, transparent 75%, rgba(255, 255, 255, 0.3) 75%, rgba(255, 255, 255, 0.3));
                              background-image: linear-gradient(-45deg, transparent 25%, rgba(255, 255, 255, 0.3) 25%, rgba(255, 255, 255, 0.3) 50%, transparent 50%, transparent 75%, rgba(255, 255, 255, 0.3) 75%, rgba(255, 255, 255, 0.3));
                              background-size: 40px 40px;
                              -webkit-animation: progress 8s linear infinite;
                              animation: progress 8s linear infinite;
                            }
                            progress::-moz-progress-bar {
                              border-radius: 5px;
                              background-color: '.$color.';
                              background-image: linear-gradient(-45deg, transparent 25%, rgba(255, 255, 255, 0.3) 25%, rgba(255, 255, 255, 0.3) 50%, transparent 50%, transparent 75%, rgba(255, 255, 255, 0.3) 75%, rgba(255, 255, 255, 0.3));
                              background-size: 40px 40px;
                              -moz-animation: progress 8s linear infinite;
                              animation: progress 8s linear infinite;
                            }
                        </style>';
            $printJauge .= '<p><progress id="avancement" value="'.$ratio.'" max="100"></progress></p>';
            }
            $printJauge .= '</div><div class="clear"></div>';
        
        } else if($type == 2 && $valueObjectif>1) {
            
            $printJauge = '        
                    <div id="wpspo-cadre">
                        <div id="wpspo-cadre-infos">'.$textProjet.'</div>';
            if($display==1) {
                
                $printJauge .= '
                    <script src="'.WP_PLUGIN_URL.'/'.WPSPO_NAME_DIR.'/wpspo-js/fancyBox/lib/jquery-1.9.0.min.js"></script>
                    <script src="'.WP_PLUGIN_URL.'/'.WPSPO_NAME_DIR.'/wpspo-js/jquery.knob.js"></script>';
                $printJauge .= '<div id="graph-'.$id.'" style="text-align: center;margin-top:5px;">';
                $printJauge .= "<script>
                        jQuery(document).ready(function() { 
                            $('.dial').knob({
                             'min':0,
                             'max':100,
                             'readOnly': true,
                             'fgColor': '".$color."',
                             'dynamicDraw': true,
                             'tickColorizeValues': true
                            });
                        });
                        
                    </script>";
                $printJauge .= '<input class="dial" data-thickness=".4" data-width="80%" value="'.$ratio.'%" /></div>';
            }
            $printJauge .= '</div>
                    <div class="clear"></div>
        ';
        } else {
            
            $printJauge = '
                <div id="wpspo-cadre">
                        <div id="wpspo-cadre-infos">'.$textProjet.'</div>
                </div>
                <div class="clear"></div>
            ';
        
        }

        return  $printJauge;
    
    }
}
add_shortcode( 'wpspo-jauge', 'wpspo_jauge_shortcode' );

